package com.ayoka.ui.pay;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.ayoka.R;
import com.ayoka.base.BaseActivity;
import com.ayoka.bean.WechatPaymentBean;
import com.ayoka.databinding.ActivityPayBinding;
import com.ayoka.mvp.PayMVP.IPayView;
import com.ayoka.mvp.PayMVP.PayPresenter;
import com.ayoka.utils.CountDownEvent;
import com.ayoka.utils.WeChatManager;
import com.jcp.baselibrary.model.ToolBarOptions;
import com.jcp.baselibrary.utils.DateUtil;
import com.jcp.baselibrary.utils.StringUtil;
import com.jcp.baselibrary.utils.ToastUtil;
import com.jcp.baselibrary.utils.UIUtils;

import java.util.Observable;

/**
 *                             _ooOoo_
 *                            o8888888o
 *                            88" . "88
 *                            (| -_- |)
 *                            O\  =  /O
 *                         ____/`---'\____
 *                       .'  \\|     |//  `.
 *                      /  \\|||  :  |||//  \
 *                     /  _||||| -:- |||||-  \
 *                     |   | \\\  -  /// |   |
 *                     | \_|  ''\---/''  |   |
 *                     \  .-\__  `-`  ___/-. /
 *                   ___`. .'  /--.--\  `. . __
 *                ."" '<  `.___\_<|>_/___.'  >'"".
 *               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *               \  \ `-.   \_ __\ /__ _/   .-` /  /
 *          ======`-.____`-.___\_____/___.-`____.-'======
 *                             `=---='
 *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                     佛祖保佑        永无BUG
 *                              佛曰:
 *                   写字楼里写字间，写字间里程序员；
 *                   程序人员写程序，又拿程序换酒钱。
 *                   酒醒只在网上坐，酒醉还来网下眠；
 *                   酒醉酒醒日复日，网上网下年复年。
 *                   但愿老死电脑间，不愿鞠躬老板前；
 *                   奔驰宝马贵者趣，公交自行程序员。
 *                   别人笑我忒疯癫，我笑自己命太贱；
 *                   不见满街漂亮妹，哪个归得程序员？
*/
/**
 * =========================================================
 * @ProjectName:
 * @ClassName: PayActivity
 * @Description: 支付类
 * @Author: jincp
 * @CreateDate: 2019/3/14 13:39
 * =========================================================
 */
public class PayActivity extends BaseActivity<ActivityPayBinding, PayPresenter> implements IPayView {

    public static void start(Activity activity, long orderId) {
        Intent intent = new Intent(activity, PayActivity.class);
        intent.putExtra("OrderId", orderId);
        activity.startActivity(intent);
    }

    private long orderId;

    @Override
    protected void configIntentValue() {
        orderId = getIntent().getLongExtra("OrderId", 0);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_pay;
    }

    @Override
    protected void initViews() {
        mDataBinding.btnWechatPay.setOnClickListener(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions();
        options.navigateId = 0;
        options.backString = getString(R.string.cancel);
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        CountDownEvent.getInstance().addObserver(this);
        CountDownEvent.getInstance().start(15 * 60 * 1000);

        mDataBinding.tvPrice.setText(String.valueOf(499));
        mDataBinding.tvOrderCode.setText(StringUtil.formatString(R.string.format_order_code, "ayoka20190310120943794"));
    }

    @Override
    protected PayPresenter getPresenter() {
        return new PayPresenter();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == mDataBinding.btnWechatPay) {
            mPresenter.getetPaymentData(orderId);
        }
    }


    @Override
    public void onGetPaymentDataSuccess(WechatPaymentBean wechatPaymentBean) {
        if (wechatPaymentBean == null) {
            ToastUtil.getInstance().show(getString(R.string.get_data_failure));
            return;
        }
        WeChatManager.getInstance().addObserver(this);
        WeChatManager.getInstance().startPay(wechatPaymentBean);
    }


    @Override
    public void update(Observable observable, Object data) {
        super.update(observable, data);
        if (observable instanceof CountDownEvent) {
            long leftTime = (long) data;
            if (leftTime > 0) {
                String text = StringUtil.formatString(R.string.format_surplus_time_for_pay,DateUtil.formatTime(leftTime / 1000));
                mDataBinding.tvSurplusTimeForPay.setText(text);
            } else {
                mDataBinding.tvSurplusTimeForPay.setText(R.string.over_pay_time);
            }
        } else if (observable instanceof WeChatManager) {
            int operation = (int) data;
            if (WeChatManager.WE_CHAT_PAY_START == operation) {
                showWaitDialog(getString(R.string.wechat_pay_start));
            } else if (WeChatManager.WE_CHAT_PAY_SUCCESS == operation) {
                ToastUtil.getInstance().show(getString(R.string.wechat_pay_success));
                hideWaitDialog();
                handlePaySuccess();
            } else if (WeChatManager.WE_CHAT_PAY_FILED == operation) {
                ToastUtil.getInstance().show(getString(R.string.wechat_pay_failure));
                hideWaitDialog();
            } else if (WeChatManager.WE_CHAT_PAY_CANCEL == operation) {
                ToastUtil.getInstance().show(getString(R.string.wechat_pay_cancel));
                hideWaitDialog();
            }
            mDataBinding.btnWechatPay.setEnabled(true);
        }
    }

    private void handlePaySuccess() {
        finish();
    }

    @Override
    public void onDestroy() {
        CountDownEvent.getInstance().deleteObserver(this);
        WeChatManager.getInstance().deleteObserver(this);
        super.onDestroy();
    }
}
